package com.example.apple.testdown.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.apple.testdown.MyApplication;
import com.example.apple.testdown.entity.Person;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.HttpHandler;

import java.util.List;

/**
 * 作    者：高学军
 * 时    间：16/7/26
 * 描    述：
 * 修改时间：
 */
public class DownService extends Service{


    FinalHttp fh;

    public static HttpHandler handler;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        fh = new FinalHttp();

        if(null == intent){
            return START_STICKY;
        }


        //查找数据库
        List<Person> personList = MyApplication.db.findAll(Person.class,"statue = "+Person.DOWNLOGIN);

        //如果 personList size >0;并且size == 1就开始下载,否则啥都不用管
        //这样做的目的是就为service只开启一次去下载,其余的下载都昂在队列里面去下载,只有当下载成功一个其余的才会去下载
        if(personList.size() > 0 || personList.size() == 1){
            getVideo(personList.get(0));
        }

        return START_STICKY;
    }


    //查找数据库正在下载的数据
    public void Select(){

        //查找数据库
        List<Person> personList = MyApplication.db.findAll(Person.class,"statue = "+Person.DOWNLOGIN);


        //如果 personList size >0;就开始下载,否则啥都不用管
        if(personList.size() > 0){
            getVideo(personList.get(0));
        }

    }

    public void getVideo(final Person person){



        handler = fh.download(person.getUrl(), person.getPath(), true,
                new AjaxCallBack() {

                    @Override
                    public void onFailure(Throwable t, String strMsg) {
                        // TODO


                        List<Person> personList = MyApplication.db.findAll(Person.class, "userid = " + person.getUserid());

                        Person person1 = personList.get(0);
//                        Person person1 = MyApplication.db.findById("userid = "+person.getUserid(),Person.class);
                        person1.setStatue(Person.DOWNLOGIN);
                        MyApplication.db.update(person1);


                        Select();

                    }

                    @Override
                    public void onLoading(long count, long current) {

                        Log.d("current===", current + "=="+count);
                        //发送当前进度
                        Intent intent = new Intent();
                        intent.putExtra(MyApplication.VID, person.getUserid());
                        intent.putExtra(MyApplication.DOWNLOADED, current);
                        intent.putExtra(MyApplication.FULLSIZE, count);
                        intent.setAction(MyApplication.COURSE_RECEIVER_ACTION);// action与接收器相同
                        sendBroadcast(intent);

                    }

                    @Override
                    public void onSuccess(Object t) {
                        super.onSuccess(t);


                        List<Person> personList = MyApplication.db.findAll(Person.class, "userid = " + person.getUserid());

                        //这样做的目的是为了防止数组越界异常
                        if(personList.size()==0){
                            return;
                        }
                        Person person1 = personList.get(0);

//                        Person person1 = MyApplication.db.findById("id = "+person.getId(),Person.class);
                        person1.setStatue(Person.COMPLETE);
                        MyApplication.db.update(person1);

                        Select();




                    }

                });

    }

}
